<template>
  <div class="userAdd">
    <h2>添加用户</h2>
    <el-form :model="user">
      <el-form-item label="用户编码">
        <el-input v-model="user.userCode"></el-input>
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input  v-model="user.userName"></el-input>
      </el-form-item>
      <el-form-item label="用户密码">
        <el-input  v-model="user.userPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input  v-model="user.rePwd"></el-input>
      </el-form-item>
      <el-form-item label="出生日期">
      <el-date-picker
          v-model="user.birthday"
          type="date"
          placeholder="选择日期时间"
          align="right">
      </el-date-picker>
      </el-form-item>
      <el-form-item label="手机号码">
        <el-input  v-model="user.phone"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input  v-model="user.address"></el-input>
      </el-form-item>
      <el-form-item label="角色">
      <el-select  v-model="user.userRole" placeholder="角色">
        <el-option v-for="role in roleList" :label="role.roleName" :value="role.id"></el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="身份证正面图片">
        <el-upload
            class="avatar-uploader"
            action="http://192.168.232.128:8090/upload"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :before-upload="beforeUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {roleList,userAdd} from "@/api";

export default {
  name: "UserAdd",
  data(){
    return{
      imageUrl:"",
      user:{},
      roleList:[]
    }
  },
  methods:{
    beforeUpload(file){ //在上传之前 文件类型判断
      // const isJPG = file.type === 'image/jpeg';
      const isPNG =  file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2; //小于2MB

      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isPNG && isLt2M ;
    },
    uploadSuccess(res,file){
      this.user.idPicPath =  res.data;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    onSubmit(){
      let that = this;
      userAdd(that.user)
      .then(res=>{
        if(res.data){
          that.$message({
            message: '添加成功',
            type: 'success',
            onClose:function (){
              //消息关闭时候
              that.$router.push({
                path:"/userlist"
              });
            }
          });
        }
      })
      .catch(err=>{
        that.$message("操作失败")
      })
    }
  },
  mounted() {
    let that = this;
    roleList()
    .then(res=>{
      that.roleList =  res.data.data;
    })
    .catch(err=>{

    })
  }
}
</script>

<style >

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>